var express = require('express');
var router = express.Router();
const addressSchema = require('../module/address');
const jwt = require('jsonwebtoken');
let index = 0
let secret = 'houduan'
//获取收货地址列表
router.post('/getaddresslist', function (req, res, next) {
    let { token } = req.body
    try {
        let data = jwt.verify(token, secret)
        let id = data.username
        addressSchema.find({ id }, (err, doc) => {
            if (err) throw err
            if (doc.length) {
                res.json({
                    status: 0,
                    msg: "获取成功",
                    data: doc[0].result
                })
            } else {
                res.json({
                    status: 1,
                    msg: "无数据,请增加收获地址"
                })
            }
        })
    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }

});

//添加收获地址
router.post('/addressadd', function (req, res, next) {
    try {
        findOneAndSetFalse(req, res, next);
    } catch (err) {
        console.log("error ={}", err);
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }

});

async function findOneAndSetFalse(req, res, next) {
    let { token, name, phone, prov, city, country, detailAddress, isDefault } = req.body
    let data = jwt.verify(token, secret)
    try {
        let id = data.username
        //查找是否有默认地址
        if (isDefault === "true") {
        
        addressSchema.update({id,"result.isDefault":"true"},{$set:{"result.$.isDefault":"false"}},(err,doc)=>{
           
            console.log("第二步执行");
            if (err) throw err
        })
        }
        
        let time1 = new Date()
        let time = time1.getTime()
        console.log("isDefault={}", isDefault);
        //添加地址
        addressSchema.updateOne({ id },
            { $push: { result: { name, phone, prov, city, country, detailAddress, isDefault, index: time } } },
            { upsert: true },
            (err, doc) => {
                if (err) throw err;
                res.json({
                    status: 0,
                    msg: "添加成功"
                })
                console.log("第三步执行");
            })
    } catch (error) {
        console.log("11111111111111111111error", error);
    }
}
//编辑收获地址

router.post('/addressedit', function (req, res, next) {
    // let { token, name, phone, prov, city, country, detailAddress, isDefault, index } = req.body
    try {
        edit(req, res, next)
    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }

});
async function edit(req, res, next) {
    let { token, name, phone, prov, city, country, detailAddress, isDefault, index } = req.body
    let data = jwt.verify(token, secret)
    console.log(isDefault)
    try {
        //如果有默认则其他全部改为不默认
        let id = data.username
        if (isDefault === "true") {
        console.log("第1步执行");
        addressSchema.update({id,"result.isDefault":"true"},{$set:{"result.$.isDefault":"false"}},(err,doc)=>{
            console.log("第二步执行");
            if (err) throw err
        })
        }
       
        //修改id跟index一样的值
        addressSchema.update({id,"result.index":index},{$set:{'result.$':{name, phone, prov, city, country, detailAddress, isDefault, index }}}, (err, doc) => {
            console.log("第3步执行");
            if (err) throw err
            res.json({
                status: 0,
                msg: "修改成功",
                doc: doc
            })
        })
    } catch (error) {
        console.log("11111111111111111111error", error);
    }
}
//删除地址
router.post('/addressremove', function (req, res, next) {
    let { token, index } = req.body
    try {
        let data = jwt.verify(token, secret)
        let id = data.username
        addressSchema.findOne({id},(err,doc)=>{
            console.log(doc)
            doc.update({$pull:{result:{index:index}}},(err,doc)=>{
                if (err) throw err
                res.json({
                    status:0,
                    msg:"删除成功",
                })
            })
        })
        
    } catch (error) {
        res.json({
            status: 4,
            msg: "token过期，请重新登入"
        })
    }

});





module.exports = router;